<?php
class CulturalMosaicController extends Controller {
        
    function __construct() {
        
        if (isset($_SESSION['userID'])) {
            $login = new Login();
            if (!$login->get($_SESSION['userID']))
                header("Location: ".SITE_URL.'?c=Login');
        }
        else {
            header("Location: ".SITE_URL.'?c=Login');
        }
        
        parent::__construct();
        
    }
    
	function index() {
            
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            $this->vars['section'] = "CulturalMosaic";
            $this->vars['title'] = "List of questions and answers of 'Cultural Mosaic'";
            
            $array_menus = array();
        
            $filter = array();
            $filter['parent_id'] = 0;
            $filter['type'] = 1;
            Menu::getMenuList($array_menus, $filter);
            
            $this->vars['array_menus'] = $array_menus;
            
            $question = new Question();
            $question->addSelect();
            $question->addSelect("question.id question_id,
                                    question.content question_title,
                                    question.title answer_content");
            $question->addWhere("question_type_id = 2");
            $question->find();
            
            $this->vars['question'] = $question;
            
            $this->display('CulturalMosaic_list');
        }
        
        function addQuestion() {
            
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            $cfer = new Cfer(array(
            _LB_QUESTION => SITE_URL.'?c=CulturalMosaic',
            _LB_ADD_QUESTION => SITE_URL.'?c=CulturalMosaic&a=addQuestion'));
            
            $this->vars['section'] = "CulturalMosaic";
            $this->vars['title'] = "Add a question and answer of 'Cultural Mosaic'";
            $this->vars['backlink'] = SITE_URL.'?c=CulturalMosaic';
            
            $act = isset($_POST['act']) ? $_POST['act'] : false;
            
            $question = new Question();
            
            if ($act == ACT_VALID_EDIT) {
                
                $question->title = isset($_POST['answer']) ? $_POST['answer'] : '';
				$question->content = isset($_POST['question']) ? $_POST['question'] : '';
                $question->question_type_id = 2;
				
				if ($question->content == '')
					MessageHandler::add('Missing question');
					
				if ($question->title == '')
					MessageHandler::add('Missing answer');
					
				if (MessageHandler::countError() == 0) {
					$question->insert();
					header("Location: ".SITE_URL.'?c=CulturalMosaic');
				}
            
			}

            $this->vars['question'] = $question;
            
            $this->display('CulturalMosaic_form');
        }
        
        function editQuestion() {
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            if (!isset($_GET['id']))
                header("Location: ".SITE_URL.'?c=CulturalMosaic');
            
            $id = $_GET['id'];
            
            $cfer = new Cfer(array(
            _LB_QUESTION => SITE_URL.'?c=CulturalMosaic',
            _LB_ADD_QUESTION => SITE_URL.'?c=CulturalMosaic&a=editQuestion&id='.$id));
            
            $this->vars['section'] = "CulturalMosaic";
            $this->vars['title'] = "Update an existing question of 'Cultural Mosaic'";
            $this->vars['backlink'] = SITE_URL.'?c=CulturalMosaic';
            
            $act = isset($_POST['act']) ? $_POST['act'] : false;
            
            $question = new Question();
            $question->get($id);
            
            if ($act == ACT_VALID_EDIT) {
                
                $question->title = isset($_POST['answer']) ? $_POST['answer'] : '';
				$question->image = 'no_picture';
                $question->question_type_id = 2;
                $question->content = isset($_POST['question']) ? $_POST['question'] : '';
				
				if ($question->content == '')
					MessageHandler::add('Missing question');
					
				if ($question->title == '')
					MessageHandler::add('Missing answer');
					
				if (MessageHandler::countError() == 0) {
					$question->update();
					header("Location: ".SITE_URL.'?c=CulturalMosaic');
				}
                
            }

            $this->vars['question'] = $question;
            
            $this->display('CulturalMosaic_form');
        }
        
        function deleteQuestion() {
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            if (!isset($_GET['id']))
                header("Location: ".SITE_URL.'?c=CulturalMosaic');
            
            $id = $_GET['id'];
            
            $act = isset($_POST['act']) ? $_POST['act'] : false;
            
            $question = new Question();
            
            if (!$question->get($id))
                header("Location: ".SITE_URL.'?c=CulturalMosaic');
            
            $question->delete();
            
            header("Location: ".SITE_URL.'?c=CulturalMosaic');
        }
        
}

